import 'package:common_utils/common_utils.dart';

class TimeFormat extends DateUtil {
  /// * 设置日期格式
  /// * [timeStr] 必填|String|时间内容
  /// * [format] 选填|String|时间格式|参考DateFormats类
  static setTimeFormat(String timeStr, {String format}) {
    return DateUtil.formatDateStr(timeStr, format: format);
  }

  /// * 获取系统时间
  /// * [format] 选填|String|要求返回的时间格式
  static getSystemTime({String format = DataFormats.full}){
    return DateUtil.formatDate(DateTime.now(), format: format);
  }

  /// * 根据当前时间格式Pickers插件需要的格式
  /// * 返回值：Map格式
  static pickersTimeFormat(){
    int year, month, day, hour, minute, second;

    // 获取系统时间
    String currentTime = TimeFormat.getSystemTime();

    // 将数据按条件拆分并在合并成一维数组
    List l = currentTime.split(" ");
    List arrTem = [...l[0].split("-"),...l[1].split(":")];

    // 根据条件分别赋值year, month, day, hour, minute, second
    for(int i = 0; i < arrTem.length; i++) {
      switch(i){
        case 0:
          year = int.parse(arrTem[i]);
          break;
        case 1:
          month = int.parse(arrTem[i]);
          break;
        case 2:
          day = int.parse(arrTem[i]);
          break;
        case 3:
          hour = int.parse(arrTem[i]);
          break;
        case 4:
          minute = int.parse(arrTem[i]);
          break;
        case 5:
          second = int.parse(arrTem[i]);
          break;
      }
    }

    // 返回数据
    return { "year": year, "month":month, "day":day, "hour":hour, "minute":minute, "second":second };

  }

}

/// * 定义时间格式
class DataFormats {
  /// *  [yyyy-MM-dd HH:mm:ss]
  static const String full = "yyyy-MM-dd HH:mm:ss";
  /// *  [yyyy-MM-dd HH:mm]
  static const String y_mo_d_h_m = "yyyy-MM-dd HH:mm";
  /// *  [yyyy-MM-dd]
  static const String y_mo_d = "yyyy-MM-dd";
  /// *  [yyyy-MM]
  static const String y_mo = "yyyy-MM";
  /// *  [MM-dd]
  static const String mo_d = "MM-dd";
  /// *  [MM-dd HH:mm]
  static const String mo_d_h_m = "MM-dd HH:mm";
  /// *  [HH:mm:ss]
  static const String h_m_s = "HH:mm:ss";
  /// *  [HH:mm]
  static const String h_m = "HH:mm";
  /// *  [yyyy年MM月dd日 HH时mm分ss秒]
  static const String zh_full = "yyyy年MM月dd日 HH时mm分ss秒";
  /// *  [yyyy年MM月dd日 HH时mm分]
  static const String zh_y_mo_d_h_m = "yyyy年MM月dd日 HH时mm分";
  /// *  [yyyy年MM月dd日]
  static const String zh_y_mo_d = "yyyy年MM月dd日";
  /// *  [yyyy年MM月]
  static const String zh_y_mo = "yyyy年MM月";
  /// *  [MM月dd日]
  static const String zh_mo_d = "MM月dd日";
  /// *  [MM月dd日 HH时mm分]
  static const String zh_mo_d_h_m = "MM月dd日 HH时mm分";
  /// *  [HH时mm分ss秒]
  static const String zh_h_m_s = "HH时mm分ss秒";
  /// *  [HH时mm分]
  static const String zh_h_m = "HH时mm分";
}
